//const data= require('../../../../lib/data')
class areaColumnConfiguration {
    static readArea(stream){
        let area = {};
        let offset = stream.offset;
        let buffer = stream.buffer;
        
        let string = this.readString(buffer, offset);
        area.name = string.data;
        offset = string.offset;
        area.director = buffer.readUInt8(offset);
        offset +=1;
        
        const count = buffer.readUInt16LE(offset);
        offset += 2;
        
        area.columns = [];
        let stm =  {"buffer":buffer, "offset":offset, "data":null}
        for (let i = 0; i < count; ++i) {
            
            stm = this.readColumn(stm);
            area.columns.push(stm.data);
            offset = stm.offset;
        }
            
        return {"buffer":buffer, "offset":offset, "data":area};
    }
    static readColumn(stream){
        let column = {};
        let offset = stream.offset;
        let buffer = stream.buffer;


        let string = this.readString(buffer, offset);
        column.name = string.data;
        offset = string.offset;


        
        column.area = buffer.readUInt8(offset);
        offset +=1;

        
        column.index = buffer.readUInt8(offset);
        offset +=1;
        
        column.type = buffer.readUInt8(offset);
        offset +=1;

        column.layers = buffer.readUInt8(offset);
        offset +=1;


        let left = this.readString(buffer, offset);
        column.left = left.data;
        offset = left.offset;
        
        
        let right = this.readString(buffer, offset);
        column.right = right.data;
        offset = right.offset;
        
       

        return {"buffer":buffer, "offset":offset, "data":column};
    
    }
    static readString(buffer, offset){
        let length = buffer.readUInt16LE(offset);
        offset+=2;
        let string = Buffer.alloc(length);
        buffer.copy(string, 0, offset,offset+length)
       // console.log("#"+length);
        offset += length;
        
       // console.log("#"+offset);
       // console.log(string);
        return {"buffer":buffer, "offset":offset, "data":string.toString()};
    }
    static readRoom(stream){
        let room = {};
        let offset = stream.offset;
        const buffer = stream.buffer;
         

        let string = this.readString(buffer,offset)
        room.name = string.data;
        offset = string.offset;
        room.index = buffer.readUInt8(offset);
        offset += 1;
        room.temperature = buffer.readUInt16LE(offset);
        offset += 2;
       
        room.humidity = buffer.readUInt8(offset);
        offset += 1;
        let count = buffer.readUInt16LE(offset);
        offset += 2;
        room.areas = [];
        let stm =  {"buffer":buffer, "offset":offset, "data":null}
        for (let i = 0; i < count; ++i) {
            stm = this.readArea(stm);
            room.areas.push(stm.data);
            offset = stm.offset; 
        }

        return {"buffer":buffer, "offset":offset, "data":room};
    }  
  static read(buffer) {
    let offset = 0;
    const count = buffer.readUInt16LE(0);
    offset += 2;
    const data = {};
    data.rooms = [];
    
    let stm = { "buffer":buffer,"offset": offset, "data":null};
    for(let i = 0; i< count; ++i){
        stm = this.readRoom(stm);
        data.rooms.push(stm.data);
        offset = stm.offset; 
   }
 
    console.log(buffer);
    return data;

  }
  static type(){
    return "areaColumnConfiguration"
  }
  static command(){
    return Buffer.from([0xb0, 0x0],"latin1").readInt16LE()
  }
}

module.exports = areaColumnConfiguration